package com.luchao.back.service.impl;

import com.luchao.back.mapper.ShoePhotosMapper;
import com.luchao.back.mapper.ShoeSizesMapper;
import com.luchao.back.mapper.ShoesMapper;
import com.luchao.back.pojo.*;
import com.luchao.back.service.ShoesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

@Service
public class ShoesServiceImpl implements ShoesService {

    @Autowired
    private ShoesMapper shoesMapper;

    @Autowired
    private ShoeSizesMapper shoeSizesMapper;

    @Autowired
    private ShoePhotosMapper shoePhotosMapper;

    @Override
    public List<Shoes> getShoesByPage(int pageNum, int pageSize, List<Integer> sizeids, List<Integer> sbids, List<Integer> stids, List<Integer> scids, List<String> sgenders) {
        return shoesMapper.getShoesByPage((pageNum - 1) * pageSize, pageSize, sizeids, sbids, stids, scids, sgenders);
    }

    @Override
    public long getShoesCount(List<Integer> sizeids, List<Integer> sbids, List<Integer> stids, List<Integer> scids, List<String> sgenders) {
        return shoesMapper.getShoesCount(sizeids, sbids, stids, scids, sgenders);
    }

    @Override
    public List<Types> getShoesTypesByPage(int pageNum, int pageSize) {
        return shoesMapper.getShoesTypesByPage((pageNum - 1) * pageSize, pageSize);
    }

    @Override
    public List<Brands> getShoesBrandsByPage(int pageNum, int pageSize) {
        return shoesMapper.getShoesBrandsByPage((pageNum - 1) * pageSize, pageSize);
    }

    @Override
    public List<Color> getShoesColorsByPage(int pageNum, int pageSize) {
        return shoesMapper.getShoesColorsByPage((pageNum - 1) * pageSize, pageSize);
    }

    @Override
    public List<String> getShoesProducerByPage(int pageNum, int pageSize) {
        return shoesMapper.getShoesProducerByPage((pageNum - 1) * pageSize, pageSize);
    }

    @Override
    @Transactional
    public boolean editShoes(Shoes shoes) {
        boolean flag = true;
        try {
            int es = shoesMapper.editShoes(shoes);
            if (es < 0) {
                throw new Exception();
            }
            List<ShoeSizes> addShoeSizes = new ArrayList<ShoeSizes>();
            for (ShoeSizes shoeSizes : shoes.getShoeSizes()) {
                if (shoeSizes.getSsid() > -1) {
                    shoeSizesMapper.editShoesSizes(shoeSizes);
                } else {
                    addShoeSizes.add(shoeSizes);
                }
            }
            if (addShoeSizes.size() > 0) {
                int ass = shoeSizesMapper.addShoeSizes(shoes.getSid(), addShoeSizes);
                if (ass < 0) {
                    throw new Exception();
                }
            }

            List<ShoePhotos> addShoePhotos = new ArrayList<ShoePhotos>();
            for (ShoePhotos shoePhotos : shoes.getShoePhotos()) {
                if (shoePhotos.getSpid() < 0) {
                    addShoePhotos.add(shoePhotos);
                }
            }
            if (addShoePhotos.size() > 0) {
                int asp = shoePhotosMapper.addShoePhotos(shoes.getSid(), addShoePhotos);
                if (asp < 0) {
                    throw new Exception();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
        } finally {
            return flag;
        }
    }

    @Override
    public boolean batchChangeState(List<Shoes> list) {
        return shoesMapper.batchChangeState(list) > 0 ? true : false;
    }

    @Override
    public List<Shoes> getAllShoes() {
        return shoesMapper.getAllShoes();
    }

    @Override
    public int checkShoes(String sname) {
        return shoesMapper.checkShoes(sname);
    }

    @Override
    public int checkSnum(String snum) {
        return shoesMapper.checkSnum(snum);
    }

    @Override
    @Transactional
    public boolean deletePhoto(String path, Integer sid, String fileName) {
        boolean flag = true;
        try {
            if (sid > 0) {
                shoePhotosMapper.deleteShoePhotos(sid, fileName);
            }

            File file = new File(path + fileName);
            if (!file.exists()) {
                flag = false;
            } else {
                file.delete();
            }
        } catch (Exception e) {
            flag = false;
        } finally {
            return flag;
        }
    }

    @Override
    @Transactional
    public boolean addShoes(Shoes shoe) {
        boolean flag = true;
        try {
            int i = shoesMapper.createShoes(shoe);
            if (i > 0) {
                shoeSizesMapper.addShoeSizes(shoe.getSid(), shoe.getShoeSizes());
                shoePhotosMapper.addShoePhotos(shoe.getSid(), shoe.getShoePhotos());
            } else {
                flag = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
        } finally {
            return flag;
        }
    }

    @Override
    public Shoes getShoesById(Integer sid) {
        return shoesMapper.getShoesById(sid);
    }

    @Override
    public List<Shoes> getShoesByBrands(Integer bid) {
        return shoesMapper.getShoesByBrands(bid);
    }

    @Override
    public List<Shoes> getShoesTop(String sgender, String topWay) {
        return shoesMapper.getShoesTop(sgender, topWay);
    }
}
